<?php
dl("php_w32.dll");
define("__STDCALL",	0);
define("__CDECL",	1);
class windll
{
	private $dll;
	private $hmodule;
	private $func_addr;
	private $func_type;
	/**
	 * function type
	 * __stdcall 0
	 * __cdecl   1
	 */

	function __construct($dll = false,$func_type = 0)
	{
		if($dll) $this->load($dll,$func_type);
	}
	function load($dll,$func_type = 0)
	{
		$this->dll = $dll;
		$this->func_type = $func_type;
		$this->func_addr = array();
		return $this->hmodule = w32_load_lib($dll);
	}
	function free()
	{
		w32_free_lib($this->hmodule);
		$this->hmodule = false;
		$this->func_addr = array();
	}
	function __call($name,$arg)
	{
		if(!$this->hmodule)
		{
			trigger_error('no dll loaded!',E_USER_WARNING);
			return NULL;
		}
		if(!isset($this->func_addr[$name]))
		{
			$func_addr = w32_get_func_addr($this->hmodule,$name);
			if($func_addr)
				$this->func_addr[$name] = $func_addr;
			else
			{
				trigger_error(sprintf("%s() was not found!",$name),E_USER_WARNING);
				return NULL;
			}
		}
		if( isset($arg[0]) && is_array($arg[0]) )
			return w32_call_func($this->func_addr[$name],$arg[0],
				isset($arg[1])? (int)$arg[1] : $this->func_type);
		else
			return w32_call_func($this->func_addr[$name],$arg,$this->func_type);
	}
	function __destruct()
	{
		if($this->hmodule) w32_free_lib($this->hmodule);
	}
}